﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using CrmField.Plugins;
using Microsoft.Xrm.Sdk;

namespace CrmField.Tests
{
    [TestClass]
    public class WNSClientTest
    {
        [TestMethod]
        public void SendMessage()
        {
            var ps = new System.Security.PermissionSet(System.Security.Permissions.PermissionState.Unrestricted);
            ps.AddPermission(new System.Net.WebPermission( System.Net.NetworkAccess.Connect,@"^http[s]?://(?!((localhost[:/])|(\[.*\])|([0-9]+[:/])|(0x[0-9a-f]+[:/])|(((([0-9]+)|(0x[0-9A-F]+))\.){3}(([0-9]+)|(0x[0-9A-F]+))[:/]))).+"));
            ps.AddPermission(new System.Security.Permissions.SecurityPermission( System.Security.Permissions.PermissionState.None));
            //ps.AddPermission(new System.Net.WebPermission(System.Security.Permissions.PermissionState.));
            ps.PermitOnly();

            //var channelURI = @"https://db3.notify.windows.com/?token=AgYAAABFrpb%2fN8CNk1Y746AT9hwPnTb61Wtw93%2bg7Dq1K7x955PzPp%2b7DrBUTkG7zcVMGiOsD%2boJwjvY%2bEDp7MXnK%2bA226o5772qc2ILD6rx0DWG51nR1QhegfZ3KEa9HTGkSY4%3d";
            var channelURI = @"https://db3.notify.windows.com/?token=AgYAAAD2memLCAKhIF2gcMnTcs0RIwtmTQo%2bH%2f1s6fEqJvLKFFnQYWyuJDXRA0yHImpQFhHgy5ttciVKi7LeyJapmATqrjG5OaeZtZHZQgDfuiWow8Q%2bmuAAQLq82WNRXcL1nPU%3d";
            //var sid = "ms-app://s-1-15-2-790713484-1473554138-849783067-1476057168-1843449078-3911949915-1426145489";
              var sid = "ms-app://s-1-15-2-790713484-1473554138-849783067-1476057168-1843449078-3911949915-1426145489";
            //var clientSecret = "13Yg+2IsKE5hDxjZ+bBwh3zIckK34qZy";
              var clientSecret = "13Yg+2IsKE5hDxjZ+bBwh3zIckK34qZy";

            Entity target = new Entity("ServiceAppointment");
            var Subject = (target.Contains("subject") ? target["subject"] : "No Subject");
            var Location = (target.Contains("subject") ? target["location"] : "No Location");
            var Time = string.Format("{0:ddd MMMM dd, yyyy @ HH:mm}", target.Contains("subject") ? target["scheduledstart"] : DateTime.Now);
            var xml = "<toast>";
            xml += "<visual>";
            xml += "<binding template=\"ToastText04\">";
            xml += string.Format("<text id=\"1\">{0}</text>", Subject);
            xml += string.Format("<text id=\"2\">{0}</text>", Location);
            xml += string.Format("<text id=\"3\">{0}</text>", Time);
            xml += "</binding>";
            xml += "</visual>";
            xml += "</toast>";

            xml= "<toast><visual><binding template=\"ToastText04\"><text id=\"1\">Test</text><text id=\"2\">kt2 6qs</text><text id=\"3\">Sun January 27, 2013 @ 15:30</text></binding></visual></toast>";

            var contentType = "text/xml";
            var notificationType = "wns/toast";
            
            WNSClient client = new WNSClient();
            client.OnLog += client_OnLog;
            client.PostToWns(clientSecret, sid, channelURI, xml, notificationType, contentType);
        }

        void client_OnLog(string message)
        {
            Console.WriteLine(message);
        }
    }
}
